<?php 
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/
/**
 * @author: Colin Hom
 * 
 **/
sfLoader::loadHelpers("Partial");
class sentSpamComponents extends sfComponents{

	public function executeSendInvite(){
		
	      $annotate_email = $this->user->getEmail();
	      $annotate_sig = $this->user->getUsername();
	      $annotate_api = new AnnotateApi();
	      $annotate_api->createAccount($annotate_email, $annotate_sig);
	
	      // now we need a user activation object for this account
	      $activation = new UserActivation();
	      
	      $token = substr(md5(time()),4,11);		// our token is the 11 characters after the first four of a md5 hash of the current unix timestamp
		  $activation->setToken($token);			// set the token
		  $activation->setUserId($this->user->getId());	// set the user_id
		  
		  $activation->save();		// save object to the database
	      
	      // now what we need to do is generate an activation url to email the user      
	      $activation_url = $this->generateUrl('activation', array('id'=>$activation->getId(), 'token'=>$activation->getToken()), true);
	      
	      // retrieve the smtp server from the app.yml
		  $smtp_server = sfConfig::get('app_smtp_server');
		  $smtp_user = sfConfig::get('app_smtp_user');
		  $smtp_password = sfConfig::get('app_smtp_password');
		  
		  $this->mailContext['url'] = $activation_url;
		  try
		  {
		    // Create the mailer
		    $connection = new Swift_Connection_SMTP($smtp_server, 465, Swift_Connection_SMTP::ENC_SSL);
		    $connection->setUsername($smtp_user);
		    $connection->setPassword($smtp_password);
		    $mailer = new Swift($connection);  
		  
		  	// Subject line
		  	$message = new Swift_Message('Welcome to FinalsClub.org');
		 
		  	// Render message parts - do a network check to determine which message
		  	$message->attach(new Swift_Message_Part(get_partial("sentSpam/".$this->template."Html", $this->mailContext), 'text/html'));		// HTML e-mail body
		  	$message->attach(new Swift_Message_Part(get_partial("sentSpam/".$this->template."Plain", $this->mailContext), 'text/plain'));	// Plaintext e-mail body
		 
		  	// Set the 'To' and 'From' fields
		  	$mailer->send($message, $this->user->getEmail(), $smtp_user);
		  	$mailer->disconnect();
		  	$this->message = "Success";
		  	SentSpamPeer::logJoinSpam($this->user->getId(),$this->tag);
		  	echo "TESTSTST";
		  }
		  catch (Exception $e)
		  {		echo "<br>".$e->getMessage()."<br>";	
		  		echo $e->getTraceAsString();
				$this->message = "Failed";
		  }
		 
		 
		  	
		  	
	}		
}

?>